const service = require('../services/main');
const {logger} = require('../services/logger');
// get block info 
let getBitTransanctions = async (ctx, next) => {
    try {

        // TODO get info from DB
        let txid = ctx.query.txid
        if (txid == undefined) {
            ctx.body = { status: "0000", errCode: "10001", msg: 'txid is be requeired' }
            return;
        }
        let result = await service.getBitTransanctions(txid);
        let obj = {};
        let arr = [];
        result.forEach(element => {
            obj.id = element.id;
            obj.txid = element.txid;
            obj.hash = element.hash;
            obj.version = element.version;
            obj.size = element.size;
            obj.locktime = element.locktime;
            obj.vin = JSON.parse(element.vin);
            obj.vout = JSON.parse(element.vout);
            obj.hex = element.hex;
            arr.push(obj);
        });
        ctx.body = { status: "0000" }
        ctx.body.data = arr;
    } catch (error) {
        logger.error("getBitTransanctions error===>", error);
    }
    next();
}

module.exports = { getBitTransanctions }